/**
 *
 */
package com.kandinfo.survey.patient.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.common.dto.widget.DataTablesResponseDto;
import com.kandinfo.survey.common.dto.PatientDto;
import com.kandinfo.survey.common.dto.PatientSearchConditionDto;
import com.kandinfo.survey.patient.service.PatientManageService;

/**
 * 家属病人关联逻辑处理controller
 * 
 * @author Patrick
 */
@Controller
@RequestMapping(value = "/patientmanage")
public class PatientManageController {

    @Autowired
    PatientManageService patientManageService;

    /**
     * 病患管理入口
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return "views/patient/patientmanage/patient-list";
    }

    /**
     * 条件查询，返回list
     * 
     * @param conditionDto
     * @return
     */
    @RequestMapping(value = "find-by-condition", method = RequestMethod.POST)
    public DataTablesResponseDto<PatientDto> getList(@RequestBody PatientSearchConditionDto conditionDto) {
        DataTablesResponseDto<PatientDto> response = new DataTablesResponseDto<>();
        List<PatientDto> data = patientManageService.findByCondition(conditionDto);

        response.setData(data);
        response.setTotalDisplayRecords(data == null ? 0L : new Long(data.size()));
        response.setTotalRecords(data == null ? 0L : new Long(data.size()));
        return response;
    }

    /**
     * 病患详细信息入口(patientId 即 userId,userType为病人)
     */
    @RequestMapping(value = "/detail/{patientId}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable Long patientId) {
        return "views/patient/patientmanage/patient-detail";
    }
    
    /**
     * 通过ID获取数据
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    private ResultDto get(Model model, @RequestParam("id") Long id) {
        if (patientManageService.findDto(id) == null) {
            return ResultDtoFactory.toNack("查询失败!");
        }
        return ResultDtoFactory.toAck("查询成功!", patientManageService.findDto(id));
    }

    /**
     * 通过ID或ID List获取数据
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private ResultDto list(@RequestParam("idList[]") List<Long> idList) {
        if (patientManageService.findDtos(idList) == null) {
            return ResultDtoFactory.toNack("查询失败!");
        }
        return ResultDtoFactory.toAck("查询成功!", patientManageService.findDtos(idList));
    }

}
